<script src="<?= base_url()?>public/js/jquery.min.js"></script>
<form action="../setpermis/<?= $group_id?>" method="post" id='PermisForm'>
<table style="border-collapse: collapse;" borderColor=#666666 height=40 width=600px cellPadding=1 align=center border=1>
    <thead>
        <tr style='background-color: #dedede;'>
            <th><input type='checkbox' id='allbox'></th>
            <th nowrap>权限名</th>
            <th nowrap>权限</th>
            <th nowrap>级别</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($permis_module_list as $key=>$permis_module) {
            if(isset($permis_list[$key])){
                $is_checked = 'checked';
            }else{
                $is_checked = '';
            }
        ?>
            <tr style='text-align: center;' pid="<?= $key?>">
                <td>
                    <input type='checkbox' name='permis_module_id[]'
                        tag value='<?= $key?>' <?=$is_checked?>
                            parent_id="<?= $permis_module['info']['parent_id']?>">
                    <input type='hidden' name='permis_module[<?= $key?>][name]'
                        value='<?= $permis_module['info']['name']?>'>
                    <input type='hidden' name='permis_module[<?= $key?>][parent_id]'
                        value='<?= $permis_module['info']['parent_id']?>'>
                    <input type='hidden' name='permis_module[<?= $key?>][type]'
                        value='<?= $permis_module['info']['type']?>'>
                </td>
                <td nowrap><?= $permis_module['info']['remark']?></td>
                <td nowrap><?= $permis_module['info']['name']?></td>
                <td nowrap>一级</td>
            </tr>
        <?php
            if(isset($permis_module['list'])){
                foreach ($permis_module['list'] as $key=>$module) {
                    if(isset($permis_list[$key])){
                        $is_checked = 'checked';
                    }else{
                        $is_checked = '';
                    }
        ?>
                    <tr style='text-align: center;' pid="<?= $key?>">
                        <td>
                            <input type='checkbox' name='permis_module_id[]'
                                tag value='<?= $key?>' <?=$is_checked?>
                                    parent_id="<?= $module['info']['parent_id']?>"
                                    list_id="<?= $module['info']['list_id']?>">
                            <input type='hidden' name='permis_module[<?= $key?>][name]'
                                value='<?= $module['info']['name']?>'>
                            <input type='hidden' name='permis_module[<?= $key?>][parent_id]'
                                value='<?= $module['info']['parent_id']?>'>
                            <input type='hidden' name='permis_module[<?= $key?>][type]'
                                value='<?= $module['info']['type']?>'>
                        </td>
                        <td><?= $module['info']['remark']?></td>
                        <td><?= $module['info']['name']?></td>
                        <td>二级</td>
                    </tr>
        <?php
                }
            }
        }
        ?>
        <tr>
            <td colspan="4" style='text-align: center;'>
                <input type="button" id='submitBtn' value='保存'>
                <input type="button" id='cancelBtn' value='返回'>
            </td>
        </tr>
    </tbody>
</table>
</form>
<script>
$(function(){
    //全选、取消全选
    $('#allbox').on('click',function(){
        $('input[type=checkbox][tag]').prop('checked',$(this).prop('checked'));
    });
    $('input[type=checkbox][tag]').on('click',function(){
        var parent_id = $(this).attr('parent_id');
        var pid = $(this).val();
        if(0 == parent_id){//一级模块
            if(!$(this).prop('checked')){//取消一级模块，则一并取消所有二级模块权限
                $('input[type=checkbox][parent_id='+pid+']').prop('checked',$(this).prop('checked'));
            }
        }else{//二级模块
            if($(this).prop('checked')){//选择二级模块后，需一并选择一级模块
                $('input[type=checkbox][value='+parent_id+']').prop('checked',$(this).prop('checked'));
            }
            if($(this).attr('list_id') && $(this).attr('list_id') > 0){//关联操作
                var list_id = $(this).attr('list_id');
                $(this).parent().parent().siblings('tr[pid='+list_id+']').
                    find('input[type=checkbox]').prop('checked',$(this).prop('checked'));
            }
        }
    });
    var is_submit = 1;
    $('#submitBtn').on('click',function(){
        var idx = 1;
        if(1 != is_submit){
            return false;
        }
        is_submit = 2;
        $('#PermisForm').submit();
    });
    $('#cancelBtn').on('click',function(){
        location.href = '../index';
    });
});
</script>